# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def preorderTraversal(self, root):
        # result = []
        # stack = [root]
        # while stack:
        #     node = stack.pop()
        #     if not node:
        #         continue
        #     else:
        #         result.append(node.val)
        #     if node.right:
        #         stack.append(node.right)
        #     if node.left:
        #         stack.append(node.left)
        # return result
        result= []
        def dfs(root):
            if not root: return
            result.append(root.val)
            dfs(root.left)
            dfs(root.right)
        dfs(root)
        return result